#!/bin/bash

SUDO="sudo "
if [[ $(id -u) -eq 0 ]]; then SUDO=""; fi

read -p "Enter ketadb version (default 1.4.6 e.g. 1.x.x.x .etc): " version </dev/tty
version=$(echo "$version" | tr -s '[:space:]')
if [[ "$version" == "" ]]; then version="1.4.6"; fi

read -p "Enter ketadb node name (default node-01): " node_name </dev/tty
node_name=$(echo "$node_name" | tr -s '[:space:]')
if [[ "$node_name" == "" ]]; then node_name="node-01"; fi

if [[ ! -f /tmp/ketadb-$version.tar.gz ]]; then
  rm -rf /tmp/ketadb-$version
  read -p "ketadb package [/tmp/ketadb-$version.tar.gz] not found, download it with wget? (Y/n)" download </dev/tty
  download=$(echo "$download" | tr -s '[:space:]')
  if [[ "$download" != [Nn] ]]; then
    version_dir="${version}"
    version_parts=`echo "${version}" | tr -cd "." | wc -c`
    if [[ $version_parts > 2 ]]; then
        version_dir=${version%.*}
    fi

    wget https://phoenix-public-1312700048.cos.ap-shanghai.myqcloud.com/ketadb-release/${version_dir}.x/ketadb/keta-v${version}-apps.tar.gz -O /tmp/ketadb-$version.tar.gz
  else
    echo "please put ketadb package into /tmp/ketadb-$version.tar.gz";
    exit 0;
  fi
fi

if [[ ! -d /tmp/ketadb-$version ]]; then
  mkdir -p /tmp/ketadb-$version
  tar zxvf /tmp/ketadb-$version.tar.gz -C /tmp/ketadb-$version --strip-components=1
  if [[ $? != 0 ]]; then rm -rf /tmp/ketadb-$version; exit -1; fi
fi

$SUDO mkdir -p /opt/ketadb/${node_name}

if [[ -x /opt/ketadb/$node_name/bin/keta ]]; then
    read -p "ketadb[${node_name}] is installed, reinstall it? (y/N) " reinstall </dev/tty
    reinstall=$(echo "$reinstall" | tr -s '[:space:]')
    if [[ "$reinstall" != [Yy] ]]; then exit 0; fi
fi

ps -ef | grep '/opt/ketadb/'${node_name} | grep -v grep
if [[ $? == 0 ]]; then $SUDO systemctl stop ketadb-${node_name}; fi

$SUDO rm -rf /tmp/ketadb/${node_name}/config
$SUDO mkdir -p /tmp/ketadb/${node_name}/config

if [[ -f /opt/ketadb/${node_name}/config/keta.yml ]]; then
  $SUDO cp -r /opt/ketadb/${node_name}/config/* /tmp/ketadb/${node_name}/config/
fi

if [[ ! -f /tmp/ketadb/${node_name}/config/keta.yml ]]; then
  read -p "Enter ketadb[${node_name}] http port (default 9200): " ketadb_http_port </dev/tty
  ketadb_http_port=$(echo "$ketadb_http_port" | tr -s '[:space:]')
  if [[ "$ketadb_http_port" == "" ]]; then ketadb_http_port="9200"; fi

  read -p "Enter ketadb[${node_name}] transport port (default 9300): " ketadb_transport_port </dev/tty
  ketadb_transport_port=$(echo "$ketadb_transport_port" | tr -s '[:space:]')
  if [[ "$ketadb_transport_port" == "" ]]; then ketadb_transport_port="9300"; fi

  read -p "Enter ketadb[${node_name}] data path (default /tmp/ketadb-data/"$node_name"): " ketadb_data_path </dev/tty
  ketadb_data_path=$(echo "$ketadb_data_path" | tr -s '[:space:]')
  if [[ "$ketadb_data_path" == "" ]]; then ketadb_data_path="/tmp/ketadb-data/"$node_name; fi

  read -p "Enter ketadb[${node_name}] logs path (default /tmp/ketadb-logs/"$node_name"): " ketadb_logs_path </dev/tty
  ketadb_logs_path=$(echo "$ketadb_logs_path" | tr -s '[:space:]')
  if [[ "$ketadb_logs_path" == "" ]]; then ketadb_logs_path="/tmp/ketadb-logs/"$node_name; fi

  read -p "Enter ketadb[${node_name}] mysql host (default localhost): " ketadb_mysql_host </dev/tty
  ketadb_mysql_url=$(echo "$ketadb_mysql_url" | tr -s '[:space:]')
  if [[ "$ketadb_mysql_url" == "" ]]; then ketadb_mysql_url="localhost"; fi

  read -p "Enter ketadb[${node_name}] mysql port (default 3306): " ketadb_mysql_port </dev/tty
  ketadb_mysql_port=$(echo "$ketadb_mysql_port" | tr -s '[:space:]')
  if [[ "$ketadb_mysql_port" == "" ]]; then ketadb_mysql_port="3306"; fi

  read -p "Enter ketadb[${node_name}] mysql database (default ketadb): " ketadb_mysql_db </dev/tty
  ketadb_mysql_db=$(echo "$ketadb_mysql_db" | tr -s '[:space:]')
  if [[ "$ketadb_mysql_db" == "" ]]; then ketadb_mysql_db="ketadb"; fi

  read -p "Enter ketadb[${node_name}] mysql user (default root): " ketadb_mysql_user </dev/tty
  ketadb_mysql_user=$(echo "$ketadb_mysql_user" | tr -s '[:space:]')
  if [[ "$ketadb_mysql_user" == "" ]]; then ketadb_mysql_user="root"; fi

  read -p "Enter ketadb[${node_name}] mysql password (default 654321): " ketadb_mysql_pass </dev/tty
  ketadb_mysql_pass=$(echo "$ketadb_mysql_pass" | tr -s '[:space:]')
  if [[ "$ketadb_mysql_pass" == "" ]]; then ketadb_mysql_pass="654321"; fi

  $SUDO mkdir -p $ketadb_data_path $ketadb_logs_path /tmp/ketadb/${node_name}/config
  cat << EOF | $SUDO tee /tmp/ketadb/${node_name}/config/keta.yml
cluster.name: default
node.name: ${node_name}
http.port: ${ketadb_http_port}
transport.tcp.port: ${ketadb_transport_port}
network.host: 0.0.0.0
keta.environment: local
keta.database.mysql.url: ${ketadb_mysql_host}:${ketadb_mysql_port}
keta.database.mysql.database: ${ketadb_mysql_db}
keta.database.mysql.user: ${ketadb_mysql_user}
keta.database.mysql.password: ${ketadb_mysql_pass}
path.data: ${ketadb_data_path}
path.logs: ${ketadb_logs_path}
EOF
fi

$SUDO rm -rf /opt/ketadb/${node_name}/*
$SUDO rm -rf /etc/systemd/system/ketadb-${node_name}.service

$SUDO cp -r /tmp/ketadb-$version/* /opt/ketadb/$node_name/
$SUDO cp /tmp/ketadb-$version/.commit /opt/ketadb/$node_name/
$SUDO cp -r /tmp/ketadb/${node_name}/config/* /opt/ketadb/${node_name}/config/
$SUDO chown -R ${USER} /opt/ketadb/${node_name}

cat << EOF | $SUDO tee /etc/systemd/system/ketadb-${node_name}.service
[Unit]
Description=KetaDB Service - ${node_name}

[Service]
Restart=on-failure
RestartSec=30s
User=${USER}
ExecStart=/opt/ketadb/${node_name}/bin/keta

[Install]
WantedBy=multi-user.target
Alias=ketadb-${node_name}.service
EOF

echo -e "\nstart ketadb service => "
echo -e "\n\tsudo systemctl start ketadb-${node_name}"